<?php
class Object{
	/**
	 * получаем все объекты
	 */
	public static function getAllObject($type = NULL){
		if ($type == NULL)
			$stmtWhere = '';
		else 
			$stmtWhere = "WHERE `object`.`type` = " . $type;
			
		$objDB = Service_Database::instance();
		$stmt = "
		SELECT `object`.`ID_object`,
					 `object`.`name`,
					 `object`.`annotation`,
					 `object`.`price`,
					 `object`.`date_first`,
					 `object`.`date_last`,
					 `object`.`currency`,
					 `object`.`type`,
					 `images`.`title` as `imageTitle`,
					 `images`.`name` as `imageName`
		FROM `object`
		LEFT JOIN `images` ON `images`.`ID_image` = `object`.`ID_image`
		 " . $stmtWhere . " 
		ORDER BY `date_first`";
		$arrObject = $objDB->select($stmt);
		
		$arrSortObject = array();
		foreach ($arrObject as $arrObject) 
			$arrSortObject[$arrObject['type']][] = $arrObject;
		
		ksort($arrSortObject);			
		return $arrSortObject;
	}
	
	/**
	 * получаем объект
	 */
	public static function getObject($idObject){
		$objDB = Service_Database::instance();
		$stmt = "
		SELECT *
		FROM `object`
		WHERE `object`.`ID_object` = " . $idObject;
		return $objDB->selectOne($stmt);			
	}
	
	/**
	 * добаляем объект
	 *
	 */
	public static function addObject($arrObject){
		$objDB = Service_Database::instance();
		$stmt = "
		INSERT INTO `object` (`name`,`text`, `annotation`, `price`, `date_first`, `date_last`, `currency`, `type`)
		VALUES ('" . $arrObject['name'] . "', '" . $arrObject['text'] . "', '" . $arrObject['annotation'] . "', '" . $arrObject['price'] . "', '" . $arrObject['date_first'] . "', '" . $arrObject['date_last'] . "', '" . $arrObject['currency'] . "', " . $arrObject['type'] . ")";
		return $objDB->queryInsertAutoincrement($stmt);
	}
	
	/**
	 * обновляем инофрмацию об объекте
	 *
	 */
	public static function updateObject($arrObject){
		$objDB = Service_Database::instance();
		$stmt = "
		UPDATE `object`
		SET `name` = '" . $arrObject['name'] . "', 
				`text` = '" . $arrObject['text'] . "', 
				`annotation` = '" . $arrObject['annotation'] . "', 
				`price` = '" . $arrObject['price'] . "', 
				`date_first` = '" . $arrObject['date_first'] . "', 
				`date_last` = '" . $arrObject['date_last'] . "', 
				`currency` = " . $arrObject['currency'] . ", 
				`type` = " . $arrObject['type'] . "
		WHERE `object`.`ID_object` = " . $arrObject['ID_object'];
		return $objDB->query($stmt);
	}
	
	/**
	 * Удаляем объект
	 *
	 */
	public static function deleteObject($idObject){
		$objDB = Service_Database::instance();
		$objGallery = new Gallery();
		$objGallery->deleteEssenceImages(1, $idObject);
		$stmt = "
		DELETE
		FROM `object`
		WHERE `object`.`ID_object` = " . $idObject;
		return $objDB->query($stmt);
	}
	
	/**
	 * получаем объекты в столбец
	 */
	public static function getObjectToColumn($num, $type){
		$objDB = Service_Database::instance();
		$stmt = "
		SELECT `object`.`ID_object`,
					 `object`.`name`,
					 `object`.`annotation`,
					 `images`.`title` as `imageTitle`,
					 `images`.`name` as `imageName`
		FROM `object`
		LEFT JOIN `images` ON `images`.`ID_image` = `object`.`ID_image`
		WHERE `object`.`type` = " . $type . "
		ORDER BY RAND()
		LIMIT 3";
		return $objDB->select($stmt);
	}
}
?>